# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
import supeanut_config
from CommonLib.mylog import mylog
from CommonLib.FileTool.CsvTool import CsvTool


'''
作者：supeanut
创建时间：2016-xx-xx xx:xx:xx
功能描述：
	从sina min 的csv数据中抽取日线数据
	15：10后运行
	指定日期区间，并验证规范
相关配置：
	supeanut_config.XXX
历史改动：
	2016-xx-xx: xxxxxx
'''
class SinaDayDataSupport:
	def __init__(self):
		self.csvtool = CsvTool()
		self.mindata_path = supeanut_config.SINA_MINDATA_CSV_PATH
		self.field = supeanut_config.SINA_MINDATA_FIELD
		self.field_time_index = supeanut_config.SINA_MINDATA_TIME_INDEX
		self.day_path = supeanut_config.SINA_DAYDATA_CSV_PATH

	# 从每个code文件抽取末尾行record，组成当前日期的全市场日线数据
	# 包括了：列数检验
	def extract(self, date, ignore_error = True):
		log = mylog('DataSupport.SinaDayDataSupoort.extract', None)
		records = []
		data_path = os.path.join(self.mindata_path, date)
		if not os.path.exists(data_path):
			return False, 'no such dir: %s' % data_path
		csv_list = os.listdir(data_path)
		for csv in csv_list:
			# 检验列对齐
			flag, msg = self.csvtool.verify_csv(data_path, csv, len(self.field))
			if flag is False:
				log.error(msg)
				if ignore_error is True:
					continue
				else:
					return False, msg
			# 读取csv到list
			flag, msg = self.csvtool.read_csv(data_path, csv)
			if flag is False:
				log.error(msg)
				if ignore_error is True:
					continue
				else:
					return False, msg
			# 除去非本日期的数据
			msg2 = []
			for item in msg:
				if item[7] == date:
					msg2.append(item)
			msg = msg2
			if len(msg) == 0:
				continue
			# 按照time排序
			msg.sort(key = lambda k:k[self.field_time_index])
			last_record = msg[-1]
			records.append(last_record)
		return True, records
	
	# colume数据格式检验
	# 收盘时间检验
	# 去除time一栏
	# 排序成DayData标准数据集顺序
	def verify(self, day_data):
		log = mylog('DataSupport.SinaDayDataSupoort.verify', None)
		# 注意此格式与配置文件SINA_MINDATA_FIELD极度相关，注意修改关联性
		# min的第0行，第7行。。。在day的第1,2,3行。。
		min_day_field_map = [0, 7, 1, 3, 4, 2, 5, 6]
		code_RE = re.compile(r'^((in)|(sz)|(sh))[0-9]{6}$')
		value_RE = re.compile(r'^[0-9]+(\.[0-9]*)?$')
		date_RE = re.compile(r'^[0-9]{4}-[01][0-9]-[0123][0-9]$')
		time_RE = re.compile(r'^[012][0-9]:[0-5][0-9]:[0-5][0-9]$')
		new_records = []
		for record in day_data:
			code = record[0]
			values = record[1:7]
			date = record[7]
			time = record[8]
			a = code_RE.match(code)
			# 格式检验
			if a is None:
				return False, 'stockcode error: %s'%(code)
			for value in values:
				b = value_RE.match(value)
				if b is None:
					return False, 'price or vol value error:%s in %s'%(value,code)
			c = date_RE.match(date)
			if c is None:
				return False, 'date error: %s in %s'%(date, code)
			d = time_RE.match(time)
			if d is None:
				return False, 'time error: %s in %s'%(time, code)
			# 收盘时间检验
			if time < '15:00:00':
				log.warning('time not in close：%s'%time)
				continue
			# 排成标准数据集顺序
			sort_record = []
			for field_index in min_day_field_map:
				sort_record.append(record[field_index])
			new_records.append(sort_record)
		return True, new_records
		
	# 存到本地csv
	def store(self, day_data, date):
		log = mylog('DataSupport.SinaDayDataSupoort.store', None)
		# 用到配置文件信息，注意同步
		flag, msg = self.csvtool.update_csvfile([0], day_data, self.day_path, date)
		if flag is False:
			return False, msg
		log.info('store stocks num:%d'%len(day_data))
		return True, 'suc'

if __name__ == '__main__':
	obj = SinaDayDataSupport()
	flag, msg = obj.extract('2016-12-16', False)
	print flag
	if flag is True:
		flag, msg = obj.verify(msg)
		print flag
	if flag is True:
		flag, msg = obj.store(msg, '2016-12-16')
		print flag, msg
